package com.maint.m.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.maint.m.data.model.TicketDetail
import com.maint.m.TicketApiService
import com.maint.m.TicketDetailRequest
import com.maint.m.TicketDetailResponse
import com.maint.m.NetworkUtils

class TicketDetailViewModel : ViewModel() {

    val ticketDetail = MutableLiveData<TicketDetail>()
    val isLoading = MutableLiveData<Boolean>()
    val errorMessage = MutableLiveData<String?>(null)

    fun loadTicketDetail(ticketId: String) {
        isLoading.value = true
        errorMessage.value = null

        val apiService = NetworkUtils.createService(TicketApiService::class.java)
        val call = apiService.getTicketDetail(TicketDetailRequest(ticketId))

        call.enqueue(object : retrofit2.Callback<TicketDetailResponse> {
            override fun onResponse(
                call: retrofit2.Call<TicketDetailResponse>,
                response: retrofit2.Response<TicketDetailResponse>
            ) {
                isLoading.value = false

                if (response.isSuccessful && response.body()?.statusCode == "200") {
                    val result = response.body()?.result
                    Log.i("TicketDetailViewModel", "获取到详情数据: $result")

                    if (result != null) {
                        // 初始化TicketDetail对象并设置属性
                    val modelDetail = TicketDetail()
                    modelDetail.dispatchOrderNumber = result?.dispatchOrderNumber
                    modelDetail.unit = result?.unit
                    modelDetail.serialNumber = result?.serialNumber
                    modelDetail.operationTask = result?.operationTask
                    modelDetail.status = result?.status
                    
                    // 处理操作项列表 - 从TicketConfirmationActivity借鉴的逻辑
                    val operationItems = result?.operationItem?.mapIndexed { index, content ->
                        com.maint.m.data.model.OperationItem().apply {
                            number = index + 1
                            this.content = content
                        }
                    } ?: emptyList()
                    modelDetail.operationItem = operationItems
                    
                    ticketDetail.value = modelDetail
                    } else {
                        errorMessage.value = "详情数据为空"
                    }
                } else {
                    errorMessage.value = "获取详情失败: ${response.body()?.message ?: response.message()}"
                }
            }

            override fun onFailure(call: retrofit2.Call<TicketDetailResponse>, t: Throwable) {
                isLoading.value = false
                Log.e("TicketDetailViewModel", "网络请求失败", t)
                errorMessage.value = "网络错误: ${t.message}"
            }
        })
    }

    // 提供结构化消息创建方法
    fun createStructuredMessage(): String {
        // 创建 JSON 格式的消息
        val messageMap = mapOf(
            "business" to 1
        )

        return com.google.gson.Gson().toJson(messageMap)
    }
}